package org.sofun.subscription.util;

import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.Month;
import java.util.HashSet;
import java.util.Set;

/**
 * 节假日工具类
 */
public class HolidayUtil {
    
    /**
     * 法定节假日集合（示例数据，实际应从配置或数据库读取）
     */
    private static final Set<LocalDate> LEGAL_HOLIDAYS = new HashSet<>();
    
    static {
        // 2025年法定节假日示例（实际应从配置文件或数据库加载）
        // 元旦
        LEGAL_HOLIDAYS.add(LocalDate.of(2025, Month.JANUARY, 1));
        
        // 春节假期（示例）
        LEGAL_HOLIDAYS.add(LocalDate.of(2025, Month.JANUARY, 28));
        LEGAL_HOLIDAYS.add(LocalDate.of(2025, Month.JANUARY, 29));
        LEGAL_HOLIDAYS.add(LocalDate.of(2025, Month.JANUARY, 30));
        LEGAL_HOLIDAYS.add(LocalDate.of(2025, Month.JANUARY, 31));
        LEGAL_HOLIDAYS.add(LocalDate.of(2025, Month.FEBRUARY, 1));
        LEGAL_HOLIDAYS.add(LocalDate.of(2025, Month.FEBRUARY, 2));
        LEGAL_HOLIDAYS.add(LocalDate.of(2025, Month.FEBRUARY, 3));
        
        // 清明节
        LEGAL_HOLIDAYS.add(LocalDate.of(2025, Month.APRIL, 5));
        LEGAL_HOLIDAYS.add(LocalDate.of(2025, Month.APRIL, 6));
        LEGAL_HOLIDAYS.add(LocalDate.of(2025, Month.APRIL, 7));
        
        // 劳动节
        LEGAL_HOLIDAYS.add(LocalDate.of(2025, Month.MAY, 1));
        LEGAL_HOLIDAYS.add(LocalDate.of(2025, Month.MAY, 2));
        LEGAL_HOLIDAYS.add(LocalDate.of(2025, Month.MAY, 3));
        
        // 端午节
        LEGAL_HOLIDAYS.add(LocalDate.of(2025, Month.MAY, 31));
        LEGAL_HOLIDAYS.add(LocalDate.of(2025, Month.JUNE, 1));
        LEGAL_HOLIDAYS.add(LocalDate.of(2025, Month.JUNE, 2));
        
        // 中秋节
        LEGAL_HOLIDAYS.add(LocalDate.of(2025, Month.OCTOBER, 6));
        LEGAL_HOLIDAYS.add(LocalDate.of(2025, Month.OCTOBER, 7));
        LEGAL_HOLIDAYS.add(LocalDate.of(2025, Month.OCTOBER, 8));
        
        // 国庆节
        LEGAL_HOLIDAYS.add(LocalDate.of(2025, Month.OCTOBER, 1));
        LEGAL_HOLIDAYS.add(LocalDate.of(2025, Month.OCTOBER, 2));
        LEGAL_HOLIDAYS.add(LocalDate.of(2025, Month.OCTOBER, 3));
        LEGAL_HOLIDAYS.add(LocalDate.of(2025, Month.OCTOBER, 4));
        LEGAL_HOLIDAYS.add(LocalDate.of(2025, Month.OCTOBER, 5));
    }
    
    /**
     * 判断是否为周末
     * @param date 日期
     * @return true-周末，false-非周末
     */
    public static boolean isWeekend(LocalDate date) {
        DayOfWeek dayOfWeek = date.getDayOfWeek();
        return dayOfWeek == DayOfWeek.SATURDAY || dayOfWeek == DayOfWeek.SUNDAY;
    }
    
    /**
     * 判断是否为法定节假日
     * @param date 日期
     * @return true-法定节假日，false-非法定节假日
     */
    public static boolean isLegalHoliday(LocalDate date) {
        return LEGAL_HOLIDAYS.contains(date);
    }
    
    /**
     * 判断是否为节假日（包含周末和法定节假日）
     * @param date 日期
     * @return true-节假日，false-非节假日
     */
    public static boolean isHoliday(LocalDate date) {
        return isWeekend(date) || isLegalHoliday(date);
    }
    
    /**
     * 判断是否为工作日
     * @param date 日期
     * @return true-工作日，false-非工作日
     */
    public static boolean isWorkingDay(LocalDate date) {
        return !isHoliday(date);
    }
    
    /**
     * 获取下一个工作日
     * @param date 起始日期
     * @return 下一个工作日
     */
    public static LocalDate getNextWorkingDay(LocalDate date) {
        LocalDate nextDay = date.plusDays(1);
        while (isHoliday(nextDay)) {
            nextDay = nextDay.plusDays(1);
        }
        return nextDay;
    }
    
    /**
     * 获取上一个工作日
     * @param date 起始日期
     * @return 上一个工作日
     */
    public static LocalDate getPreviousWorkingDay(LocalDate date) {
        LocalDate previousDay = date.minusDays(1);
        while (isHoliday(previousDay)) {
            previousDay = previousDay.minusDays(1);
        }
        return previousDay;
    }
    
    /**
     * 添加法定节假日（用于动态配置）
     * @param holiday 节假日
     */
    public static void addLegalHoliday(LocalDate holiday) {
        LEGAL_HOLIDAYS.add(holiday);
    }
    
    /**
     * 移除法定节假日（用于动态配置）
     * @param holiday 节假日
     */
    public static void removeLegalHoliday(LocalDate holiday) {
        LEGAL_HOLIDAYS.remove(holiday);
    }
    
    /**
     * 清空所有法定节假日（用于重新加载配置）
     */
    public static void clearLegalHolidays() {
        LEGAL_HOLIDAYS.clear();
    }
}
